package cn.cymym.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONStreamAware;

import cn.cymym.comm.CommException;
import cn.cymym.domain.AddShoppingCar;
import cn.cymym.domain.Arist;
import cn.cymym.service.ArtistService;
import cn.cymym.service.FistPageService;


@WebServlet(urlPatterns= {"/recommened.do","/selectArtist.do","/addShoppingCar.do"})
public class FirstPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(FirstPageServlet.class);
	private FistPageService fService = new FistPageService();
	private ArtistService aService = new ArtistService();
   
    public FirstPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();
		String methodname = servletPath.substring(1);
		methodname = methodname.substring(0,methodname.length()-3);
		logger.debug(methodname);
		try {
			Method method = getClass().getDeclaredMethod(methodname, HttpServletRequest.class,HttpServletResponse.class);
			method.invoke(this, request,response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CommException("方法没有找到");
		}
	}
	
	protected void recommened(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		List<Map<String,Object>> list = fService.selectInfo();
		String jsonStr = null;
		String jsonArry[] = new String[13];
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			String imgname = (String)map.get("imgname");
			float price = (float) map.get("price");
			String name = (String)map.get("name");//作者姓名
			String proName = (String)map.get("proname");
		//	Date year = (Date)map.get("year");//年份
			String note = (String)map.get("note");//画作的简介
			String texture = (String)map.get("texture");//材质
			String size = (String)map.get("size");//规格
			String productnumber = (String)map.get("productnumber");//产品编号
			float salesvolume = (float)map.get("salesvolume");//销量
			long qty = (long)map.get("qty");//库存
			float originalprice = (float)map.get("originalprice");//原价
			String prodescribe = (String)map.get("prodescribe");//描述词
			
			HttpSession session = request.getSession();
			//String userCode = (String)session.getAttribute("userCode");//用户编号
			
			jsonStr = "{\"price\":\""+price+"\",\"imgname\":\""+imgname+"\",\"name\":\""+name+"\",\"proname\":\""+proName+"\",\"note\":\""+note+"\",\"texture\":\""+texture+"\",\"size\":\""+size+"\",\"productnumber\":\""+productnumber+"\",\"salesvolume\":\""+salesvolume+"\",\"qty\":\""+qty+"\",\"prodescribe\":\""+prodescribe+"\",\"originalprice\":\""+originalprice+"\"}";
			jsonArry[i]=jsonStr;
		}
		/*for (int i = 0; i < jsonArry.length; i++) {
			System.out.println(jsonArry[i]);
		}*/
		
			String newResult = StringUtils.join(jsonArry,",");
			//System.out.println(newResult);
			
		//String PA = "{\"PA\":["+jsonArry[0]+","+jsonArry[1]+","+jsonArry[2]+","+jsonArry[3]+"]}";
		String PA = "{\"PA\":["+newResult+"]}";
		 response.setContentType("application/json");
		 PrintWriter out=response.getWriter();
		 JSONObject jsonObject = JSONObject.parseObject(PA);
         out.print(jsonObject);
	}
	
	protected void selectArtist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		List<Arist> list = aService.selectArtist();
		System.out.println(list.size());
		String jsonArr[] = new String[8];
		String jsonStr = null;
		for (int i = 0; i < list.size(); i++) {
			for(Arist arist: list) {
				String name = arist.getName();
				String imgName = arist.getImgHeader();
				String note = arist.getNote();
				String aNumber = arist.getArtistNumber();
			    jsonStr = "{\"name\":\""+name+"\",\"imgName\":\""+imgName+"\",\"note\":\""+note+"\",\"aNumber\":\""+aNumber+"\"}";
			   // System.out.println(jsonStr);
			}
			jsonArr[i] = jsonStr;
		}
		String AInfoStr = StringUtils.join(jsonArr,",");
		String AINFO = "{\"AINFO\":["+AInfoStr+"]}";
		//System.out.println(AINFO);
		JSONObject aInfo = JSONObject.parseObject(AINFO);
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		out.print(aInfo);
	}
	
	protected void addShoppingCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		//Object { imgname: "9.jpg", price: "565.0", name: "冯才志", proname: "家" }
		String imgname = request.getParameter("imgname");
		String price = request.getParameter("price");
		String proname = request.getParameter("proname");
		
		System.out.println(imgname+price+proname);
		
		AddShoppingCar aspc = new AddShoppingCar(imgname, price, proname);
		HttpSession session = request.getSession();
		session.setAttribute("ascInfo", aspc);
	}
}
